<template>
  <view class="flex-col">

    <view :class="boole?'bottom115':''">
      <!--基础信息-->
      <basic-info v-model="detailForm" :boole="boole" ref="refBasicInfo"/>

      <!--更换轮胎信息-->
      <change-tire-info v-model="detailForm" :boole="boole" ref="refChangeTireInfo"/>

      <!--更换轮胎项目明细-->
      <change-tire-detail v-model="detailForm" :boole="boole" ref="refChangeTireDetail"/>

      <!--审批流程-->
      <exam-flow v-if="flowId" :instanceId="flowId"/>
    </view>

    <view class="button-container" v-if="boole">
      <button class="btn-item" @click="submit">提 交</button>
    </view>

  </view>
</template>

<script>
import {getFindCarWorkOrderInfoVo, addReplaceTyre, getReplaceTyre} from "@/api/changeTire";
import basicInfo from "./components/basicInfo.vue";
import changeTireInfo from "./components/changeTireInfo.vue";
import changeTireDetail from "./components/changeTireDetail.vue";

export default {
  name: 'add',
  components: {basicInfo, changeTireInfo, changeTireDetail},
  computed: {},
  watch: {},
  data() {
    return {
      detailForm: {
        vinNo: "",
        carNo: "",
        brandName: "",
        seriesName: "",
        modelName: "",
        belongCityName: "",
        orderNo: "",
        leaseNum: "",
        customerName: "",
        leaseStartTime: "",
        leaseEndTime: "",
        totalBudgetPrice: "",
        registerTime: "",
        bicycleBudgetPrice: "",
        replacePrice: "",
        carSurplusPrice: "",
        lastReplaceTime: "",

        workId: '',
        expectStartTime: "",
        expectEndTime: "",
        maintenanceShop: "",
        maintenanceShopId: "",
        faultPhenomenon: "",
        maintenanceFaultDiagnosis: "",
        maintenanceQuoter: "",
        reasonForClaim: "",
        claimantReason: "",
        swappingAccessories: "",
        comprehensiveRecommendations: "",
        normalRemark: "",
        carId: "",
        oldTyreMeligile: "",
        isReplaceSpareTire: "",
        tyreBrand: "",
        tyreModel: "",
        tyreFigure: "",
        leadSpeedLevel: "",
        tyreNum: "",
        travelMeligile: '',
        totalPrice: "",
        tyreRemark: "",

        //更换轮胎反馈
        actualStartTime: "",
        actualEndTime: "",
        feedbackAdvancePayment: "",
        payType: "",
        replaceCompleteUrl: "",
        balanceUrl: "",
        replaceFeedback: "",
        annexUrl: "",
        businessType: ''
      },
      flowId: null,
      boole: false
    };
  },
  created() {
  },
  mounted() {
  },
  onLoad(op) {
    let title = "";
    this.flowId = op?.flowId;
    if (op?.type === "edit") {
      this.handleGetFindCarWorkOrderInfoVo(op?.id);
      title = "新增更换轮胎工单";
      this.boole = true;
      if (op.businessDataId && op.businessDataId !== 'null') {
        this.getReplaceTyre(op?.businessDataId, op?.id);
      }
    }
    if (op?.type === "view") {
      this.getReplaceTyre(op?.businessDataId, op?.id);
      title = "更换轮胎工单详情";
      this.boole = false;
    }
    this.detailForm.carId = op?.carId;
    uni.setNavigationBarTitle({
      title,
    });
  },
  methods: {
    //处理详情
    handleGetFindCarWorkOrderInfoVo(id) {
      getFindCarWorkOrderInfoVo(id).then(res => {
        if (res.code === 200) {
          for (let key in res.data) {
            this.$set(this.detailForm, key, res.data[key]);
          }
          this.detailForm.workId = res.data?.workId || id
        }
      })
    },

    //详情查看
    getReplaceTyre(businessDataId, id) {
      getReplaceTyre(businessDataId).then(res => {
        if (res.code === 200) {
          this.detailForm = res.data
          this.detailForm.workId = id
        }
      })
    },

    async submit() {
      try {
        let arr = [
          this.$refs.refBasicInfo.handleEffect(),
          this.$refs.refChangeTireInfo.handleEffect(),
          this.$refs.refChangeTireDetail.handleEffect()
        ];

        // 等待所有处理完成
        await Promise.all(arr);

        // 在执行异步操作前显示加载动画
        uni.showLoading();

        // 请求注册登记信息
        let res = await addReplaceTyre(this.detailForm);

        // 根据返回的状态码进行处理
        if (res.code === 200) {
          uni.$emit("refBackList", true);
          this.$modal.msgSuccess("提交成功");
          setTimeout(() => {
            this.$tab.navigateBack();
          }, 1000);
        }
      } catch (error) {
        if (typeof error === 'string') {
          this.$nextTick(() => {
            uni.pageScrollTo({
              selector: error,
            });
          });
        } else {
          console.error(error)
        }
      } finally {
        setTimeout(() => {
          uni.hideLoading()
        }, 1000)
      }
    }
  },
};
</script>

<style scoped lang=""></style>


